﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace XXS.Common.Paginate
{
    /// <summary>
    /// 此基类只用于分页信息保存,数据请在子类中处理!
    /// </summary>
    public abstract class PaginateBase
    {
        #region 字段

        protected string _action;
        protected string _currentCls;
        protected int _currentPage, _pageSize;
        protected ShowType _pageType;
        protected IDictionary<string, object> _rootValues;
        protected int _totalPage;
        protected string PageSizeName = "PageSize", PageName = "Page";
        private ControllerContext _controllerContext;

        #endregion

        #region 属性

        public abstract int TotalPage { get; protected set; }
        public abstract int SourceCount { get; }

        public string Action
        {
            get { return _action; }
            set { _action = value; }
        }

        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                if (_pageSize > 0)
                    _pageSize = value;
            }
        }

        public ShowType PageType
        {
            get { return _pageType; }
            set { _pageType = value; }
        }

        public int CurrentPage
        {
            get { return _currentPage; }
            set
            {
                if (value > 0 && value <= TotalPage)
                    _currentPage = value;
            }
        }

        public string CurrentCls
        {
            get { return _currentCls; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    _currentCls = "active";
                else
                    _currentCls = value;
            }
        }

        public ControllerContext ControllerContext
        {
            get { return _controllerContext; }
            set
            {
                _action = value.RouteData.Values["Action"].ToString();
                _controllerContext = value;
            }
        }

        #endregion

        #region 构造器

        protected PaginateBase(int pageSize)
        {
            _action = string.Empty;
            _currentCls = "active";
            _pageSize = 30;
            _currentPage = 1;
            _pageType = ShowType.MVC;
            _rootValues = new RouteValueDictionary();
            _totalPage = 1;

            PageSize = pageSize;
        }

        protected PaginateBase(int pageSize, ControllerContext cc)
            : this(pageSize)
        {
            Action = cc.RouteData.Values["Action"].ToString();
        }

        #endregion

        #region 方法

        protected virtual void SetRootValue(IDictionary<string, object> rootValue)
        {
            _rootValues = rootValue;
        }

        public virtual RouteValueDictionary GetRootValue(int page)
        {
            if (_rootValues.Any(n => n.Key == PageSizeName))
                _rootValues[PageSizeName] = PageSize;
            else
                _rootValues.Add(PageSizeName, PageSize);

            if (_rootValues.Any(n => n.Key == PageName))
                _rootValues[PageName] = page;
            else
                _rootValues.Add(PageName, page);

            if (_controllerContext != null)
            {
                var resquest = _controllerContext.RequestContext.HttpContext.Request.QueryString;
                var form = _controllerContext.RequestContext.HttpContext.Request.Form;

                foreach (string key in resquest.Keys)
                {
                    if (!_rootValues.ContainsKey(key))
                    {
                        _rootValues.Add(key, resquest[key]);
                    }
                }

                foreach (string key in form.Keys)
                {
                    if (!_rootValues.ContainsKey(key))
                    {
                        _rootValues.Add(key, form[key]);
                    }
                }
            }

            return new RouteValueDictionary(_rootValues);
        }

        public virtual RouteValueDictionary GetStaticValue()
        {
            RouteValueDictionary _temp = GetRootValue(0);
            _temp.Remove(PageName);
            return _temp;
        }

        #endregion
    }
}